package com.czy.seed.mvc.home.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.czy.seed.mvc.base.service.impl.BaseServiceImpl;
import com.czy.seed.mvc.home.controller.CommodityController;
import com.czy.seed.mvc.home.entity.Commodity;

import com.czy.seed.mvc.home.entity.Merchant;
import com.czy.seed.mvc.home.mapper.CommodityMapper;
import com.czy.seed.mvc.home.service.CommodityService;
import com.czy.seed.mvc.home.service.MerchantService;
import com.czy.seed.mvc.sys.entity.SysUser;
import com.czy.seed.mvc.sys.mapper.SysUserMapper;
import com.czy.seed.mvc.util.PrincipalUtil;
import com.czy.seed.mybatis.base.QueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liuyun
 * Created by liuyun  9:37 2017/11/23.
 */
@Service
public class CommodityImpl extends BaseServiceImpl<Commodity> implements CommodityService{

    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private CommodityService commodityService;

    @Autowired
    private MerchantService merchantService;

    public static Logger logger = LoggerFactory.getLogger(CommodityController.class);
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     *
     * @return
     */


    public Map<String ,Object> CommodityInstall_impl(HttpServletRequest request){
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try{
            String install = request.getParameter("commodity");
            Commodity commodity = JSONObject.parseObject(install,Commodity.class);
            long types=commodity.getInstall();
            if(commodity.getInstall()==2){
                commodity.setInstall(1);
            }else{
                commodity.setInstall(0);
            }
            int i = commodityService.updateByPrimaryKey(commodity);
            if(types==0) {
                //将商家入围
                Merchant merchant_ = merchantService.selectByPrimaryKey(commodity.getBusinessesId());
                QueryParams queryParams = new QueryParams(Merchant.class);
                QueryParams.Criteria criteria = queryParams.createCriteria();
                merchant_.setType(1);
                merchant_.setUpdateTime(new Date());
                merchantService.updateSelectiveByPrimaryKey(merchant_);
                //将商家账号启用
                SysUser sysUser = sysUserMapper.selectByPrimaryKey(merchant_.getUserId());
                sysUser.setEnabled(true);
                sysUser.setUpdateDt(new Date());
                sysUser.setUpdateBy(PrincipalUtil.getLoginUser().getId());
                sysUserMapper.updateByPrimaryKey(sysUser);
            }
            pageInfo.put("install",i);
            logger.info("上下架成功："+commodity.getCommodityName());
        }catch(Exception e){
            logger.error("上下架失败");
            throw new RuntimeException("商品上下架失败");
        }

        return pageInfo;
    }

}
